'Imports System.io
'Imports System.Data.OleDb
Imports System.Text.RegularExpressions
Imports System.Threading
'Imports System.Security.Cryptography
Public Class FormMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlLogin As System.Windows.Forms.Panel
    Friend WithEvents pnlStatus As System.Windows.Forms.Panel
    Friend WithEvents pnlContacts As System.Windows.Forms.Panel
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
    Friend WithEvents cmbUsername As System.Windows.Forms.ComboBox
    Friend WithEvents txtPassword As System.Windows.Forms.TextBox
    Friend WithEvents lnkRegister As System.Windows.Forms.LinkLabel
    Friend WithEvents chkAutoSignIn As System.Windows.Forms.CheckBox
    Friend WithEvents chkRememberPW As System.Windows.Forms.CheckBox
    Friend WithEvents chkRememberUN As System.Windows.Forms.CheckBox
    Friend WithEvents btnSignIn As System.Windows.Forms.Button
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents tmrStatus As System.Windows.Forms.Timer
    Friend WithEvents prgStatus As System.Windows.Forms.ProgressBar
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents tvContacts As System.Windows.Forms.TreeView
    Friend WithEvents lstGroupList As System.Windows.Forms.ListBox
    Friend WithEvents wmpCon As AxWMPLib.AxWindowsMediaPlayer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FormMain))
        Me.pnlLogin = New System.Windows.Forms.Panel
        Me.wmpCon = New AxWMPLib.AxWindowsMediaPlayer
        Me.Label3 = New System.Windows.Forms.Label
        Me.lnkRegister = New System.Windows.Forms.LinkLabel
        Me.chkAutoSignIn = New System.Windows.Forms.CheckBox
        Me.chkRememberPW = New System.Windows.Forms.CheckBox
        Me.chkRememberUN = New System.Windows.Forms.CheckBox
        Me.cmbUsername = New System.Windows.Forms.ComboBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.btnSignIn = New System.Windows.Forms.Button
        Me.txtPassword = New System.Windows.Forms.TextBox
        Me.pnlStatus = New System.Windows.Forms.Panel
        Me.prgStatus = New System.Windows.Forms.ProgressBar
        Me.Label4 = New System.Windows.Forms.Label
        Me.pnlContacts = New System.Windows.Forms.Panel
        Me.TabControl1 = New System.Windows.Forms.TabControl
        Me.TabPage1 = New System.Windows.Forms.TabPage
        Me.Button1 = New System.Windows.Forms.Button
        Me.tvContacts = New System.Windows.Forms.TreeView
        Me.TabPage2 = New System.Windows.Forms.TabPage
        Me.lstGroupList = New System.Windows.Forms.ListBox
        Me.StatusBar1 = New System.Windows.Forms.StatusBar
        Me.ContextMenu1 = New System.Windows.Forms.ContextMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.MenuItem2 = New System.Windows.Forms.MenuItem
        Me.MenuItem3 = New System.Windows.Forms.MenuItem
        Me.MenuItem4 = New System.Windows.Forms.MenuItem
        Me.tmrStatus = New System.Windows.Forms.Timer(Me.components)
        Me.pnlLogin.SuspendLayout()
        CType(Me.wmpCon, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.pnlStatus.SuspendLayout()
        Me.pnlContacts.SuspendLayout()
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlLogin
        '
        Me.pnlLogin.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlLogin.Controls.Add(Me.wmpCon)
        Me.pnlLogin.Controls.Add(Me.Label3)
        Me.pnlLogin.Controls.Add(Me.lnkRegister)
        Me.pnlLogin.Controls.Add(Me.chkAutoSignIn)
        Me.pnlLogin.Controls.Add(Me.chkRememberPW)
        Me.pnlLogin.Controls.Add(Me.chkRememberUN)
        Me.pnlLogin.Controls.Add(Me.cmbUsername)
        Me.pnlLogin.Controls.Add(Me.Label2)
        Me.pnlLogin.Controls.Add(Me.Label1)
        Me.pnlLogin.Controls.Add(Me.btnSignIn)
        Me.pnlLogin.Controls.Add(Me.txtPassword)
        Me.pnlLogin.Location = New System.Drawing.Point(0, 0)
        Me.pnlLogin.Name = "pnlLogin"
        Me.pnlLogin.Size = New System.Drawing.Size(216, 432)
        Me.pnlLogin.TabIndex = 0
        '
        'wmpCon
        '
        Me.wmpCon.ContainingControl = Me
        Me.wmpCon.Enabled = True
        Me.wmpCon.Location = New System.Drawing.Point(40, 368)
        Me.wmpCon.Name = "wmpCon"
        Me.wmpCon.OcxState = CType(resources.GetObject("wmpCon.OcxState"), System.Windows.Forms.AxHost.State)
        Me.wmpCon.TabIndex = 12
        Me.wmpCon.TabStop = False
        Me.wmpCon.Visible = False
        '
        'Label3
        '
        Me.Label3.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.Label3.ForeColor = System.Drawing.Color.Red
        Me.Label3.Location = New System.Drawing.Point(28, 52)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(168, 32)
        Me.Label3.TabIndex = 10
        Me.Label3.Text = "The username and/or password are incorrect. Please try again."
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        Me.Label3.Visible = False
        '
        'lnkRegister
        '
        Me.lnkRegister.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.lnkRegister.Location = New System.Drawing.Point(52, 276)
        Me.lnkRegister.Name = "lnkRegister"
        Me.lnkRegister.Size = New System.Drawing.Size(120, 32)
        Me.lnkRegister.TabIndex = 6
        Me.lnkRegister.TabStop = True
        Me.lnkRegister.Text = "Or you can Register for an account here."
        Me.lnkRegister.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'chkAutoSignIn
        '
        Me.chkAutoSignIn.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.chkAutoSignIn.Location = New System.Drawing.Point(44, 228)
        Me.chkAutoSignIn.Name = "chkAutoSignIn"
        Me.chkAutoSignIn.Size = New System.Drawing.Size(136, 16)
        Me.chkAutoSignIn.TabIndex = 4
        Me.chkAutoSignIn.Text = "Automatic Sign-in"
        '
        'chkRememberPW
        '
        Me.chkRememberPW.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.chkRememberPW.Location = New System.Drawing.Point(44, 204)
        Me.chkRememberPW.Name = "chkRememberPW"
        Me.chkRememberPW.Size = New System.Drawing.Size(136, 16)
        Me.chkRememberPW.TabIndex = 3
        Me.chkRememberPW.Text = "Remember Password"
        '
        'chkRememberUN
        '
        Me.chkRememberUN.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.chkRememberUN.Location = New System.Drawing.Point(44, 180)
        Me.chkRememberUN.Name = "chkRememberUN"
        Me.chkRememberUN.Size = New System.Drawing.Size(136, 16)
        Me.chkRememberUN.TabIndex = 2
        Me.chkRememberUN.Text = "Remember Me"
        '
        'cmbUsername
        '
        Me.cmbUsername.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.cmbUsername.Location = New System.Drawing.Point(44, 108)
        Me.cmbUsername.Name = "cmbUsername"
        Me.cmbUsername.Size = New System.Drawing.Size(136, 21)
        Me.cmbUsername.TabIndex = 0
        '
        'Label2
        '
        Me.Label2.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(44, 132)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(136, 24)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "Password:"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label1
        '
        Me.Label1.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(44, 84)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(136, 24)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Username:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'btnSignIn
        '
        Me.btnSignIn.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.btnSignIn.Location = New System.Drawing.Point(68, 252)
        Me.btnSignIn.Name = "btnSignIn"
        Me.btnSignIn.Size = New System.Drawing.Size(88, 24)
        Me.btnSignIn.TabIndex = 5
        Me.btnSignIn.Text = "Sign In..."
        '
        'txtPassword
        '
        Me.txtPassword.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.txtPassword.Location = New System.Drawing.Point(44, 156)
        Me.txtPassword.Name = "txtPassword"
        Me.txtPassword.PasswordChar = Microsoft.VisualBasic.ChrW(42)
        Me.txtPassword.Size = New System.Drawing.Size(136, 20)
        Me.txtPassword.TabIndex = 1
        Me.txtPassword.Text = ""
        '
        'pnlStatus
        '
        Me.pnlStatus.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlStatus.Controls.Add(Me.prgStatus)
        Me.pnlStatus.Controls.Add(Me.Label4)
        Me.pnlStatus.Location = New System.Drawing.Point(0, 0)
        Me.pnlStatus.Name = "pnlStatus"
        Me.pnlStatus.Size = New System.Drawing.Size(216, 432)
        Me.pnlStatus.TabIndex = 1
        Me.pnlStatus.Visible = False
        '
        'prgStatus
        '
        Me.prgStatus.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.prgStatus.Location = New System.Drawing.Point(28, 180)
        Me.prgStatus.Name = "prgStatus"
        Me.prgStatus.Size = New System.Drawing.Size(160, 24)
        Me.prgStatus.TabIndex = 1
        '
        'Label4
        '
        Me.Label4.Anchor = System.Windows.Forms.AnchorStyles.None
        Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label4.Location = New System.Drawing.Point(28, 68)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(160, 32)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "Logging in..."
        Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'pnlContacts
        '
        Me.pnlContacts.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.pnlContacts.Controls.Add(Me.TabControl1)
        Me.pnlContacts.Controls.Add(Me.StatusBar1)
        Me.pnlContacts.Location = New System.Drawing.Point(0, 0)
        Me.pnlContacts.Name = "pnlContacts"
        Me.pnlContacts.Size = New System.Drawing.Size(216, 432)
        Me.pnlContacts.TabIndex = 2
        Me.pnlContacts.Visible = False
        '
        'TabControl1
        '
        Me.TabControl1.Alignment = System.Windows.Forms.TabAlignment.Left
        Me.TabControl1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.TabControl1.Controls.Add(Me.TabPage1)
        Me.TabControl1.Controls.Add(Me.TabPage2)
        Me.TabControl1.Location = New System.Drawing.Point(0, 0)
        Me.TabControl1.Multiline = True
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(216, 408)
        Me.TabControl1.TabIndex = 1
        '
        'TabPage1
        '
        Me.TabPage1.Controls.Add(Me.Button1)
        Me.TabPage1.Controls.Add(Me.tvContacts)
        Me.TabPage1.Location = New System.Drawing.Point(23, 4)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(189, 400)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "Contacts"
        '
        'Button1
        '
        Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Button1.Location = New System.Drawing.Point(120, 376)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(72, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Add Friend"
        '
        'tvContacts
        '
        Me.tvContacts.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tvContacts.ImageIndex = -1
        Me.tvContacts.Location = New System.Drawing.Point(0, 0)
        Me.tvContacts.Name = "tvContacts"
        Me.tvContacts.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Online"), New System.Windows.Forms.TreeNode("Offline"), New System.Windows.Forms.TreeNode("Custom Group")})
        Me.tvContacts.PathSeparator = ""
        Me.tvContacts.Scrollable = False
        Me.tvContacts.SelectedImageIndex = -1
        Me.tvContacts.ShowLines = False
        Me.tvContacts.Size = New System.Drawing.Size(192, 376)
        Me.tvContacts.TabIndex = 0
        '
        'TabPage2
        '
        Me.TabPage2.Controls.Add(Me.lstGroupList)
        Me.TabPage2.Location = New System.Drawing.Point(23, 4)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(189, 400)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "Groups"
        '
        'lstGroupList
        '
        Me.lstGroupList.Location = New System.Drawing.Point(8, 8)
        Me.lstGroupList.Name = "lstGroupList"
        Me.lstGroupList.Size = New System.Drawing.Size(176, 173)
        Me.lstGroupList.Sorted = True
        Me.lstGroupList.TabIndex = 0
        '
        'StatusBar1
        '
        Me.StatusBar1.Location = New System.Drawing.Point(0, 410)
        Me.StatusBar1.Name = "StatusBar1"
        Me.StatusBar1.Size = New System.Drawing.Size(216, 22)
        Me.StatusBar1.TabIndex = 0
        Me.StatusBar1.Text = "StatusBar1"
        '
        'ContextMenu1
        '
        Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2, Me.MenuItem3, Me.MenuItem4})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.Text = "Send Message"
        '
        'MenuItem2
        '
        Me.MenuItem2.Index = 1
        Me.MenuItem2.Text = "Send File"
        '
        'MenuItem3
        '
        Me.MenuItem3.Index = 2
        Me.MenuItem3.Text = "View Profile"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 3
        Me.MenuItem4.Text = ""
        '
        'tmrStatus
        '
        Me.tmrStatus.Interval = 250
        '
        'FormMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(216, 430)
        Me.Controls.Add(Me.pnlContacts)
        Me.Controls.Add(Me.pnlStatus)
        Me.Controls.Add(Me.pnlLogin)
        Me.Name = "FormMain"
        Me.Opacity = 0
        Me.Text = "Codename: STIMPy!"
        Me.pnlLogin.ResumeLayout(False)
        CType(Me.wmpCon, System.ComponentModel.ISupportInitialize).EndInit()
        Me.pnlStatus.ResumeLayout(False)
        Me.pnlContacts.ResumeLayout(False)
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    'Public App As clsApp
    Public thdApp As Thread = New Thread(AddressOf SetApp)
    Public thdMain As Thread
    Public thdGetMsgBox As Thread
    'Private htMain As Hashtable = New Hashtable

    Private SHA1 As New System.Security.Cryptography.SHA1Managed
    Private ASCII As New System.Text.ASCIIEncoding
    Private re As Regex = New Regex("[A-Z]+[_|:]?")
    'Protected MessageArr() As FormMessage
    'Private htMessage As Hashtable = New Hashtable
    Public msgWinArr As ArrayList = New ArrayList
    Public htTVNodes As Hashtable = New Hashtable
    Public friends() As clsContacts
    Private Delegate Sub PanelTransDlg(ByRef panelToHide As Windows.Forms.Panel, _
      ByRef panelToShow As Windows.Forms.Panel)
    Private Delegate Sub getMsgWinDlg(ByRef window As FormMessage, _
      ByVal user As String, ByVal msg As String)
    'Private frmFriendSearch As FormAddFriend
    'Private Delegate Sub oneStrParamDlg(Byref sdata As String)
    'Private AccessCon As New OleDbConnection
    'Private fsMain As FileStream
    'Private swMain As StreamWriter
    'Private srMain As StreamReader
    'Private fileDate As String
    Private Sub setApp()
        App = New clsApp(VERSION) ', frmSplash)
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        If Thread.CurrentThread.Name Is Nothing Then Thread.CurrentThread.Name = "Main Thread"
        'playSound("StartUp")
        If bSplash Then
            frmSplash.Show()
        Else
            frmMain.Opacity = 1
            frmMain.Show()
        End If
        thdApp.Start()
        'thdApp.Join()
        'frmSplash.tmrSplash.Enabled = True
        'If App.PrevInstance Then
        For Each node As TreeNode In tvContacts.Nodes
            htTVNodes.Add(node.Text, node.Index)
        Next
        'ReDim MessageArr(0)
        'MessageArr(0) = New FormMessage
        If Not sktMain Is Nothing Then
            thdMain = New Thread(AddressOf sktMain.Connect)
            thdMain.Start()
        End If

        AddHandler sktMain.DataReceived, AddressOf dataRcvd
        'fileDate = FileDateTime(App.GetPath + "\STIMPy!.exe")


        '"File info for AES"
        'fsMain = New FileStream(App.GetPath + "\config", _
        '    FileMode.OpenOrCreate, FileAccess.ReadWrite)
        'swMain = New StreamWriter(fsMain)
        '        srMain = New StreamReader(fsMain)
        'swMain.Write(fileDate)
        'swMain.Close()
        '        srMain.Close()

        ' Search for Auto Login in Database

        ' Retrieve Remembered users
        '(and passwords if applicable) from database

        ' If one exist, then skip to Login

        ' If not then show the login panel
        Me.pnlLogin.Show()
    End Sub

    ' When the link to register is clicked, ask if they want to register online
    ' or from the program itself. Do what is selected by the user.
    Private Sub lnkRegister_LinkClicked(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
        Handles lnkRegister.LinkClicked
        ' Open web browser to the registration page.
        Process.Start("http://codenamestimpy.com/profile.php?action=add")
    End Sub

    Private Sub btnSignIn_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnSignIn.Click
        'Look to see then password and username have been entered
        If cmbUsername.Text = vbNullString Then
            MsgBox("Please enter your username!", MsgBoxStyle.OKOnly, "STIMPy! Login")
            Exit Sub
        End If
        If txtPassword.Text = vbNullString Then
            MsgBox("Please enter your password!", MsgBoxStyle.OKOnly, "STIMPy! Login")
            Exit Sub
        End If
        'if user wants to be automatically signed in from now on
        If chkAutoSignIn.Checked Then
            'Add all info to database if not allready there
            'Check for others AL users and replace AL attribute 
        End If
        'if the user wants to be remembered
        If chkRememberUN.Checked Then
            'Add user to Database if not allready there
        End If
        'if the user also wants their password remembered
        If chkRememberPW.Checked Then
            'Add Password to database if not allready there
        End If
        Dim pswd As String, buffer() As Byte
        buffer = SHA1.ComputeHash(ASCII.GetBytes(Me.txtPassword.Text))
        For Each bit As Byte In buffer
            If bit < 16 Then
                pswd += "0"
            End If
            pswd += Hex(bit).ToString
        Next
        pswd = pswd.ToLower
        'MsgBox(pswd)
        If Not sktMain Is Nothing Then sktMain.Send("LOGIN_" + Me.cmbUsername.Text + " " + pswd)
        'send information to server
        'switch to the next panel
        pnlStatus.Show()
        pnlLogin.Hide()
    End Sub

    Private Sub tmrStatus_Tick(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles tmrStatus.Tick
        'Add ability for percentage complete for login
        Debug.WriteLine("status timer ticked")
        'this automatically progresses the progress bar...for now
        With prgStatus
            If .Value = 100 Then
                .Value = 0
            Else
                .Value = .Value + 5
            End If
        End With
        'Add info for TimeOut
    End Sub

    Private Sub pnlStatus_Paint(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles pnlStatus.Paint
        If Not frmSplash Is Nothing Then
            frmSplash.Close()
            'frmSplash.Dispose()
            frmSplash = Nothing
        End If
        bSplash = Nothing
        'Start the progress timer
        tmrStatus.Start()
    End Sub

    Private Sub pnlStatus_Leave(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles pnlStatus.Leave
        'stop the progress timer
        'tmrStatus.Enabled = False
    End Sub

    Private Sub dataRcvd(ByVal data As String)
        If Thread.CurrentThread.Name Is Nothing Then Thread.CurrentThread.Name = "FormMain.dataRcvd"
        'MsgBox(data)
        Select Case re.Match(data).ToString
            Case "UPTODATE"
                'UpToDate = True
                AppNewThread.Resume()
            Case "UPDATE"
                'UpToDate = False
                'Run Updater
                sktMain.Close()
                sktMain = Nothing
                App = Nothing
                Application.Exit()
            Case "MSGFROM_"
                Dim reName As New Regex("_[a-zA-Z0-9._]+:")
                'Dim reMsg As New Regex(":*")
                Dim sFrom As String = reName.Match( _
                    data.Substring(7)).ToString
                sFrom = sFrom.Substring(1, sFrom.Length - 2)
                Dim sMsg As String = data.Substring(data.IndexOf(":") + 1)
                'reMsg.Match( _
                '    data.Substring(7)).ToString()
                'sMsg = sMsg.Substring(1)
                For Each window As FormMessage In msgWinArr
                    If window.Tag = sFrom Then
                        Dim dlg1 As New getMsgWinDlg(AddressOf getMsgWin)
                        Dim args1() As Object = {window, sFrom, sMsg}
                        Me.Invoke(dlg1, args1)
                        Exit Select
                    End If
                Next
                Dim this As FormMessage = New FormMessage(sFrom, frmMain.cmbUsername.Text)
                this.Tag = sFrom
                msgWinArr.Add(this)
                Dim dlg As New getMsgWinDlg(AddressOf getMsgWin)
                Dim args() As Object = {this, sFrom, sMsg}
                Me.Invoke(dlg, args)
                'Check to see if a message window from this user exists
                'Dim index As Int32 = IIf(htMessage.ContainsKey(sFrom), _
                '  CType(htMessage.Item(sFrom), Int32).ToString, -1)
                'If index >= 0 Then
                'MessageArr(index).Show()
                'MessageArr(index).BringToFront()
                'MessageArr(index).DoInfo(sFrom, sMsg)
                'Else
                '    index = 0
                '    MessageArr(index).Show()
                '    MessageArr(index).BringToFront()
                '    MessageArr(index).DoInfo(sFrom, sMsg)
                'End If
            Case "LOGINACCEPT", "LOGINACCEPT_"
                tmrStatus.Enabled = False
                'get friends and their status
                data = data.Substring(re.Match(data).ToString.Length)
                parseFriends(data)
                Dim iDoOfflines = data.IndexOf("OFFLINEFROM_")
                If iDoOfflines >= 0 Then
                    data = parseOfflines(data)
                    'parseGroups(data)
                End If
                Dim iGroups = data.IndexOf("GROUPS:{")
                If iGroups >= 0 Then
                    parseGroups(data.Substring(iGroups, data.Length - iGroups))
                End If
                'If data.Substring(0, 8) = "OFFLINE" Then
                'ReDim Preserve ofln(0)
                'ofln(0) = New clsOfflines
                'data = parseOfflines(data)
                'If data <> vbNullString Then parseGroups(data)
                'ElseIf data.Substring(0, 8) = "GROUPS:{" Then
                'parseGroups(data)
                'Else
                'ReDim Preserve friends(0)
                'friends(0) = New clsContacts
                'parseFriends(data)
                'If data <> vbNullString And data.Substring(0, 8) <> "GROUPS:{" Then
                'data = parseOfflines(data)
                'End If
                'If data <> vbNullString Then
                'parseGroups(data)
                'End If
                'End If
                Dim dlg As New PanelTransDlg(AddressOf panelTransition)
                Dim args() As Object = {Me.pnlStatus, Me.pnlContacts}
                Me.Invoke(dlg, args)
            Case "ONLINE:", "OFFLINE:"
                parseStatus(data)
            Case "LOGINDENY:"
                Dim dlg As New PanelTransDlg(AddressOf panelTransition)
                Dim args() As Object = {Me.pnlStatus, Me.pnlLogin}
                Me.Invoke(dlg, args)
            Case "FRIENDRESULT_"
                If frmFriendSearch Is Nothing Then
                    frmFriendSearch = New FormAddFriend
                    frmFriendSearch.Show()
                End If
                parseFriendResult(data.Substring(13))

        End Select
        'get groups
        'get offlines
        'MsgBox(data)


    End Sub

    Private Sub FormMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Me.pnlContacts.Visible Then
            sktMain.Send("LOGOFF")
        Else
            sktMain.Send("DISCONNECT")
        End If
        playSound("Close")
        sktMain.Close()
        sktMain = Nothing
        If Not App Is Nothing Then App.Close()
        App = Nothing
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        pnlLogin.Hide()
        pnlContacts.Show()
        pnlContacts.BringToFront()
        Dim NodeID As Int32 = htTVNodes.Item("Offline")
        Dim toNode As TreeNode = New TreeNode("Offline")
        frmMain.tvContacts.Nodes(NodeID).Nodes.Add("Test")
        frmMain.tvContacts.Nodes(NodeID).Collapse()
        frmMain.tvContacts.Update()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        pnlLogin.Hide()
        pnlContacts.Show()
        pnlContacts.BringToFront()
    End Sub
    Private Sub panelTransition(ByRef panelToHide As Windows.Forms.Panel, _
      ByRef panelToShow As Windows.Forms.Panel)
        frmMain.playSound(App.SoundPath + "Login")
        panelToHide.Hide()
        panelToShow.Show()
        panelToShow.BringToFront()
    End Sub

    Private Sub getMsgWin(ByRef window As FormMessage, ByVal user As String, ByVal msg As String)
        If Not window.WindowState = FormWindowState.Minimized Then
            window.Show()
            window.BringToFront()
            'window.Flash(5)
        End If
        If window.WindowState = FormWindowState.Minimized _
          Or Not window.isTopWindow Then
            frmMain.playSound(App.SoundPath + "MessageRcvd")
            window.Flash(5)
        End If
        window.DoInfo(user, msg)
    End Sub

    Private Sub tvContacts_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvContacts.DoubleClick
        If Me.tvContacts.SelectedNode Is Nothing Then Exit Sub
        Dim sUser As String = Me.tvContacts.SelectedNode.Text
        If htTVNodes.Contains(sUser) Then Exit Sub
        For Each window As FormMessage In msgWinArr
            If window.Tag = sUser Then
                window.Show()
                window.BringToFront()
                Exit Sub
            End If
        Next
        Dim this As New FormMessage(sUser, Me.cmbUsername.Text)
        'this.Tag = sUser
        this.Show()
        msgWinArr.Add(this)
        Me.tvContacts.Update()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'If frmFriendSearch Is Nothing Then
        frmFriendSearch = New FormAddFriend
        frmFriendSearch.Show()
    End Sub

    Private Sub chkAutoSignIn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAutoSignIn.CheckedChanged
        Me.chkRememberPW.Checked = True
        Me.chkRememberUN.Checked = True
    End Sub

    Private Sub chkRememberUN_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRememberUN.CheckedChanged
        If Not Me.chkRememberUN.Checked And Me.chkAutoSignIn.Checked Then Me.chkAutoSignIn.Checked = False
    End Sub

    Private Sub chkRememberPW_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRememberPW.CheckedChanged
        If Not Me.chkRememberPW.Checked And Me.chkAutoSignIn.Checked Then Me.chkAutoSignIn.Checked = False
    End Sub

    Public Sub playSound(ByVal file As String)
        Dim soundFile As String = file + ".mp3"
        Dim fso As Scripting.FileSystemObject = New Scripting.FileSystemObject
        If fso.FileExists(soundFile) Then
            Me.wmpCon.URL = soundFile
        End If
    End Sub
End Class
